import requests
from config import MOONSHOT_API_KEY

def request_KimiChat(query: str, temperature=0.3) -> str:
    """
    使用requests库调用Moonshot AI的API与Kimi进行聊天。

    :param query: 用户的查询字符串。
    :param temperature: 用于控制回答的随机性，范围从0到1。
    :return: Kimi的回答。
    """
    url = "https://api.moonshot.cn/v1/chat/completions"

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {MOONSHOT_API_KEY}"
    }

    data = {
        "model": "moonshot-v1-8k",
        "messages": [
            {"role": "system", "content": "你是 Kimi，由 Moonshot AI 提供的人工智能助手。"},
            {"role": "user", "content": query}
        ],
        "temperature": temperature
    }

    try:
        response = requests.post(url, json=data, headers=headers)
        response.raise_for_status()
        completion = response.json()
        print(completion)
        return completion['choices'][0]['message']['content']
    except Exception as e:
        return f"An error occurred: {e}"

if __name__ == "__main__":
    t = "写一个小红书文案，用20岁的女大学生口吻写一个眼镜店探店笔记，力求真实，表达出店里的卖点和细节优势。不要强调跟其他眼镜店类似的服务或设施"
    t = '写一篇绝句，描写程序员'
    request_response = request_KimiChat(t
                                        ,temperature=0.999)
    print(f"request_response:{request_response}")
